<?php

namespace App\Repository\Repositories;

use App\Repository\Repositories\Interfaces\AddressRepository;
use App\Repository\Models\Address;

/**
 * Class AddressRepositoryEloquent.
 *
 * @package namespace App\Repository\Repositories;
 */
class AddressRepositoryEloquent extends BaseRepository implements AddressRepository
{
    /**
     * 搜索
     * @var array
     */
    protected $fieldSearchable = [
    ];

    /**
     * Specify Model class name
     *
     * @return string
     */
    public function model()
    {
        return Address::class;
    }

    public function setDefault($id)
    {
        $this->applyCriteria();
        $this->applyScope();

        \DB::beginTransaction();
        try {
            $this->model->update(['is_default' => 0]);
            $this->model->where('id', $id)->update(['is_default' => 1]);
            \DB::commit();
        } catch (\Exception $e) {
            \DB::rollback(); //事务回滚;
            return false;
        }

        return true;
    }
}
